package com.chen.servlet;/*
package com.chen.servlet;

import com.chen.service.UserService;
import com.chen.utils.ChenJdbcUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet("/register")
public class RegisterUserServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        userService.strIsEmpty(name, req, resp);
        String pwd = req.getParameter("pwd");
        userService.strIsEmpty(pwd, req, resp);
        String phone = req.getParameter("phone");
        userService.strIsEmpty(phone, req, resp);
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //2.调用jdbc api 在数据库插入数据
        try {
            connection = ChenJdbcUtils.getConnection();
            ChenJdbcUtils.beginTransaction(connection);   //开启事务

            //需要判断该用户名是否已重复存在，如果存在则不能注册
            preparedStatement = connection.prepareStatement("select * from chen_user where name=?;");
            preparedStatement.setString(1, name);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                req.setAttribute("errorMsg", "用户名已存在");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }

            preparedStatement = connection.prepareStatement("select * from chen_user where phone=?;");
            preparedStatement.setString(1, phone);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                req.setAttribute("errorMsg", "手机号已存在");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }

            //如果不存在
            String insertSql = "INSERT INTO `chen`.`chen_user` (`id`, `name`, `pwd`, " +
                    "phone, is_delete)" +
                    " VALUES (null, ?, ?, ?, 0);";
            preparedStatement = connection.prepareStatement(insertSql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, pwd);
            preparedStatement.setString(3, phone);
            int result = preparedStatement.executeUpdate();//执行 insert sql 语句
            if(result <= 0){
                req.setAttribute("errorMsg", "注册失败");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            req.getRequestDispatcher("login.jsp").forward(req, resp);
            //关闭事务
            ChenJdbcUtils.commitTransaction(connection);
        }catch (Exception e){
            e.printStackTrace();
            // 如果报错需要回滚事务
            ChenJdbcUtils.rollBackTransaction(connection);
            req.setAttribute("errorMsg", "注册失败");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        }finally {
            //释放资源
            ChenJdbcUtils.closeConnection(null, preparedStatement, connection);
        }
    }
}
*/
